package com.dyrj.base.system.org.service.impl;

import com.dyrj.base.system.org.dao.TDepartmentMapper;
import com.dyrj.base.system.org.model.TDepartment;
import com.dyrj.base.system.org.service.TDepartmentService;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.base.system.utils.SystemUtil;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.vo.ResultMsg;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: TODO(系统管理-部门)
 * @author 刘超
 * @date 2019-5-12 20:08
 * @version V1.0   
 */
@Service
public class TDepartmentServiceImpl implements TDepartmentService {
	@Resource
	private TDepartmentMapper tDepartmentMapper;
	@Override
	public TDepartment selectByPrimaryKey(Long id) {
		return tDepartmentMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResultMsg add(TDepartment record) {
		record.setCreatetime(new Date());
//		Long tenantid = getUser().getTenantid();
//		record.setTenantid(tenantid);
		tDepartmentMapper.insert(record);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

	@Override
	public TDepartment info(Long id) {
		TDepartment dept = tDepartmentMapper.selectAllDeptById(id);
		SysUser sysUserp = (SysUser)SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
		List<TDepartment> tDepartments=tDepartmentMapper.selectAllTreeDept(sysUserp.getTenantid());
		List<TDepartment> menus = this.checkedMenus(tDepartments,dept.getPid());
		dept.setDeptVo(menus);
		return dept;
	}
	@Override
	public ResultMsg update(TDepartment record) {
		tDepartmentMapper.updateByPrimaryKeySelective(record);
		// 更新角色信息成功后刷新session中权限
//		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//		List<SysRole> roles = mapper.selectRoleByUserId(sysUser.getId());
//		SecurityUtils.getSubject().getSession().setAttribute(SysFrameConst.SYSTEM_ROLES, roles);

		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

	@Override
	public ResultMsg del(Long id) {
		tDepartmentMapper.deleteByPrimaryKey(id);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}
	@Override
	public List<TDepartment> selectAllTreeDept(Long tenantId) {
		List<TDepartment> menus = tDepartmentMapper.selectAllTreeDept(tenantId);
		SystemUtil.deleteDepNullChildren(menus);
		return menus;
	}
	@Override
	public List<TDepartment> selectAllDept(Long id) {
		List<TDepartment> menus = tDepartmentMapper.selectAllDept(id);
//		SystemUtil.deleteDepNullChildren(menus);
		return menus;
	}
	@Override
	public ResultMsg table(TDepartment record) {
		// PageInfo<SysMenu> page = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.findPageList(record));
		List<TDepartment> menus = tDepartmentMapper.selectAllDept(record.getId());
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, menus);
	}
	/**
	 * @Title: deleteNullChildren
	 * @Description: TODO(递归清除空children)
	 * @param menus
	 * @return List<SysMenu>    返回类型
	 */
	private List<TDepartment> checkedMenus(List<TDepartment> menus, Long perms) {
		for(TDepartment info : menus) {
			if(perms==info.getId()) {
				info.setChecked(true);
			}
			if(info.getChildren().isEmpty()) {
				info.setChildren(null);
			} else {
				this.checkedMenus(info.getChildren(), perms);
			}
		}
		return menus;
	}
}
